﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.NonlinearEquationSolvers
{
    /// <summary>
    /// This class provides the finding of roots of a polynomial by using the Newton-Raphson method.
    /// The needed first derivative will be only approximated by a small limiting value 1e-5, so 
    /// the methods is slower than the regular Newton-Raphson method.
    /// </summary>
    [Serializable]
    public class ApproximationNewtonRaphsonRootFinder : AbstractDerivativeNoNeedRootFinder
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ApproximationNewtonRaphsonRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public ApproximationNewtonRaphsonRootFinder(Polynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ApproximationNewtonRaphsonRootFinder"/> class.
        /// </summary>
        /// <param name="polynomial">The polynomial for finding the roots.</param>
        public ApproximationNewtonRaphsonRootFinder(SimplePolynomial polynomial)
            : base(polynomial)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ApproximationNewtonRaphsonRootFinder"/> class.
        /// </summary>
        /// <param name="function">The function for finding the roots.</param>
        public ApproximationNewtonRaphsonRootFinder(HardRealFunction function)
            : base(function)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ApproximationNewtonRaphsonRootFinder"/> class.
        /// </summary>
        /// <param name="function">The function for finding the roots.</param>
        public ApproximationNewtonRaphsonRootFinder(IRealFunction function)
            : base(function)
        {
        }

        /// <summary>
        /// Find one root of the function by using the Newton-Raphson method. The needed first
        /// derivative will be only approximated. The x has to be choose useful to find a root.
        /// </summary>
        /// <param name="x">The start value of the approximation.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the function.</returns>
        public double FindRoots(double x, int iterations)
        {
            return this.FindRoots(x, 1e-15, iterations);
        }

        /// <summary>
        /// Find one root of the function by using the Newton-Raphson method. The needed first
        /// derivative will be only approximated. The x has to be choose useful to find a root.
        /// </summary>
        /// <param name="x">The start value of the approximation.</param>
        /// <param name="precision">The precision of the result.</param>
        /// <param name="iterations">The number of iterations to find a root.</param>
        /// <returns>One root of the function.</returns>
        public double FindRoots(double x, double precision, int iterations)
        {
            double tempuri = 0;

            for (int i = 0; i < iterations; i++)
            {
                tempuri = x - (this.Function.SolveAt(x) / this.LimitingValueApproximation(x, 1e-5));

                if (Math.Abs(tempuri - x) < precision)
                {
                    this.NeededIterations = i;
                    this.PrecisionError = false;
                    this.RelativeError = Math.Abs(tempuri - x);

                    return tempuri;
                }

                x = tempuri;
            }

            this.PrecisionError = true;
            this.NeededIterations = iterations;
            this.RelativeError = Math.Abs(tempuri - x);

            return x;
        }

        /// <summary>
        /// Approximate the specified real function by using a small limiting value.
        /// </summary>
        /// <param name="x">The x at which the function derivative should be calculate.</param>
        /// <param name="epsilon">The epsilon of the small limiting value.</param>
        /// <returns>The specified slope at the position x.</returns>
        private double LimitingValueApproximation(double x, double epsilon)
        {
            return (this.Function.SolveAt(x + epsilon) - this.Function.SolveAt(x)) / epsilon;
        }
    }
}